import { getAttrName } from "./names";
import {
  crtButton,
  crtSelect,
  crtSwitch,
  crtText,
  crtInput,
  crtNumInput,
  crtTextArea,
} from "./cmpnts";
export function attr(inputValues = {}, updateModel = () => {}) {
  this.inputValues = inputValues;
  this.updateModel = updateModel;
}
// 特定函数：根据 key 返回配置
attr.prototype.getEles = function (key) {
  let that = this;
  let name = getAttrName(key);
  let value = that.inputValues[key];
  switch (key) {
    case "type":
      return [
        {
          name: "div",
          props: { label: "样式" },
          style: {
            "--el-button-size": "20px",
            padding: "4px 6px",
          },
          children: [
            crtButton("", "small", false, () => {
              that.inputValues[key] = "";
              that.updateModel(key);
            }),
            crtButton("primary", "small", false, () => {
              that.inputValues[key] = "primary";
              that.updateModel(key);
            }),
            crtButton("success", "small", false, () => {
              that.inputValues[key] = "success";
              that.updateModel(key);
            }),
            crtButton("info", "small", false, () => {
              that.inputValues[key] = "info";
              that.updateModel(key, that.inputValues[key]);
            }),
            crtButton("warning", "small", false, () => {
              that.inputValues[key] = "warning";
              that.updateModel(key, that.inputValues[key]);
            }),
            crtButton("danger", "small", false, () => {
              that.inputValues[key] = "danger";
              that.updateModel(key, that.inputValues[key]);
            }),
          ],
        },
      ];
    case "size":
      return [
        {
          name: "div",
          props: { label: "大小" },
          children: [
            crtButton("primary", "small", true, () => {
              that.inputValues[key] = "small";
              that.updateModel(key, that.inputValues[key]);
            }),
            crtButton("primary", "", true, () => {
              that.inputValues[key] = "";
              that.updateModel(key, that.inputValues[key]);
            }),
            crtButton("primary", "large", true, () => {
              that.inputValues[key] = "large";
              that.updateModel(key, that.inputValues[key]);
            }),
          ],
        },
      ];
    case "bind":
      let v = [
        crtSelect(
          "绑定字段",
          that.inputValues[key],
          that.inputValues["options"],
          "small",
          (newvalue) => {
            that.inputValues[key] = newvalue;
            that.updateModel(key, that.inputValues[key]);
          }
        ),
      ];
      return v;
    case "color":
      console.log("需实现颜色组件");
      return [];
    default:
      if (key.startsWith("_")) {
        return [crtText(key, that.inputValues[key])];
      }
  }
  if (_.isBoolean(value)) {
    return [
      crtSwitch(name, that.inputValues[key], (newvalue) => {
        that.inputValues[key] = newvalue;
        that.updateModel(key, that.inputValues[key]);
      }),
    ];
  } else if (_.isNumber(value)) {
    return [
      crtNumInput(name, that.inputValues[key], "small", (newvalue) => {
        that.inputValues[key] = newvalue;
        that.updateModel(key, that.inputValues[key]);
      }),
    ];
  } else if (_.isFunction(value)) {
    return [
      crtTextArea(name, name, that.inputValues[key], "small", (newvalue) => {
        that.inputValues[key] = newvalue;
        that.updateModel(key, that.inputValues[key]);
      }),
    ];
  } else if (_.isString(value)) {
    return [
      crtInput(
        name,
        name,
        that.inputValues[key],

        "small",
        (newvalue) => {
          that.inputValues[key] = newvalue;
          that.updateModel(key, that.inputValues[key]);
        }
      ),
    ];
  } else if (_.isArray(value)) {
    let arrayKey = "__array_" + key;
    that.inputValues[arrayKey] = _.join(that.inputValues[key], ",");
    return [
      crtInput(name, name, that.inputValues[arrayKey], "small", (newvalue) => {
        that.inputValues[key] = _.split(newvalue, ",");
        that.inputValues[arrayKey] = newvalue;
        that.updateModel(key, that.inputValues[key]);
      }),
    ];
  }
  return [];
};
